/*	
	Author: _______________

	GamePhysics.h

	This class will be used to manage all game collisions
	and physics. This includes collision detection, and resolution.
	Each frame we will update the collided objects velocities and
	positions accordingly.

	Students will implement this as part of their projects.
*/

#pragma once
#include "resource.h"
#include "stdafx.h"
#include "Game.h"
#include "AnimatedSprite.h"
#include "SpriteManager.h"
#include "PhysicalProperties.h"
#include "CollidableObject.h"
#include "GameAI.h"

class GamePhysics
{
	public:	
	int counter;
	CollidableObject collisions[1000];
	// WE'LL DEFINE THIS CLASS LATER
	GamePhysics() 
	{
		counter = 0;
	}
	~GamePhysics()
	{
		delete collisions;
	}
	int getCounter()
	{
		return counter;
	}
	void addCollision(CollidableObject *c)
	{
		collisions[counter++] = *c;
	}

	int killIt(int playerRow1, int playerColumn1, int playerRow2, int playerColumn2, Game *game)
	{		
		for(int i = 0; i < game->getAI()->getCounter(); i++)
		{			
			if((playerColumn1 == (int)game->getAI()->getBot(i)->getAnimatedSprite()->getPhysicalProperties()->getX()/64 ||	// Width is 64 pixels
				playerColumn2 == (int)game->getAI()->getBot(i)->getAnimatedSprite()->getPhysicalProperties()->getX()/64) &&	// Width is 64 pixels
				(playerRow1 == (int)game->getAI()->getBot(i)->getAnimatedSprite()->getPhysicalProperties()->getY()/64 ||	// Height is 64 pixels
				playerRow2 == (int)game->getAI()->getBot(i)->getAnimatedSprite()->getPhysicalProperties()->getY()/64))		// Height is 64 pixels
			{
				game->getAI()->removeBot(i);
				return i;		// which AI was killed
			}
		}		
		return -1;
	}

	int row(float y)
	{
		return y/TILE_HEIGHT;
	}
	int column(float x)
	{
		return x/TILE_WIDTH;
	}
	void update(Game *game) 
	{
		GameWorld *world = game->getWorld();
		SpriteManager *spriteManager = world->getSpriteManager();
		AnimatedSprite *player = spriteManager->getPlayer();		

		// Add gravity
		if(player->getPhysicalProperties()->getY() < WORLD_HEIGHT-3*TILE_HEIGHT)					
			player->getPhysicalProperties()->incY(5);

		
		
		int dCol, dRow, incX, incY;
		int playerColumn1, playerColumn2, playerRow1, playerRow2;
		int objColumn, objRow;
		bool isRow = false;
		for(int i = 0; i < counter; i++)
		{
			
			incX = incY = 0;
			dCol = dRow = 10000; // non-zero
			playerColumn1	= column(player->getPhysicalProperties()->getX()+1);
			playerColumn2	= column(player->getPhysicalProperties()->getX()+TILE_WIDTH-1);
			playerRow1		= row(player->getPhysicalProperties()->getY()+1);
			playerRow2		= row(player->getPhysicalProperties()->getY()+TILE_HEIGHT-1);

			objColumn	= column(collisions[i].getPhysicalProperties()->getX());
			objRow		= row(collisions[i].getPhysicalProperties()->getY());


			if(playerColumn2 <= objColumn)	// player is on the left and obj 2 is on the right (not yet collide)
			{								
				dCol = (playerColumn2 - objColumn);
				if(dCol == 0) // Colliding
				{
					incX = -5;
				}
			}
			else if(playerColumn1 >= objColumn) // player is on the right and obj 2 is on the left (not yet collide)
			{
				dCol = (objColumn - playerColumn1);
				if(dCol == 0) // Colliding		
				{
					incX = 5;
				}
			}

			if(playerRow2 <= objRow)	// player is on the top and obj 2 is at the bottom (not yet collide)
			{			
				dRow = (playerRow2 - objRow);
				if(dRow == 0) // Colliding					
				{					
					incY = -5;
				}
			}
			else if (playerRow1 >= objRow) // player is at the bottom and obj 2 is on the top (not yet collide)
			{
				dRow = (objRow - playerRow1);
				if(dRow == 0) // Colliding					
				{					
					incY = 5;
				}
			}
			
			

			
			if(dCol == 0 && dRow == 0)
			{
				player->getPhysicalProperties()->incY(incY);				
			}


		}		



		for(int i = 0; i < counter; i++)
		{
			
			incX = incY = 0;
			dCol = dRow = 10000; // non-zero
			playerColumn1	= column(player->getPhysicalProperties()->getX()+1);
			playerColumn2	= column(player->getPhysicalProperties()->getX()+TILE_WIDTH-1);
			playerRow1		= row(player->getPhysicalProperties()->getY()+1);
			playerRow2		= row(player->getPhysicalProperties()->getY()+TILE_HEIGHT-1);

			objColumn	= column(collisions[i].getPhysicalProperties()->getX());
			objRow		= row(collisions[i].getPhysicalProperties()->getY());


			if(playerColumn2 <= objColumn)	// player is on the left and obj 2 is on the right (not yet collide)
			{								
				dCol = (playerColumn2 - objColumn);
				if(dCol == 0) // Colliding
				{
					incX = -5;
				}
			}
			else if(playerColumn1 >= objColumn) // player is on the right and obj 2 is on the left (not yet collide)
			{
				dCol = (objColumn - playerColumn1);
				if(dCol == 0) // Colliding		
				{
					incX = 5;
				}
			}

			if(playerRow2 <= objRow)	// player is on the top and obj 2 is at the bottom (not yet collide)
			{			
				dRow = (playerRow2 - objRow);
				if(dRow == 0) // Colliding					
				{					
					incY = -5;
				}
			}
			else if (playerRow1 >= objRow) // player is at the bottom and obj 2 is on the top (not yet collide)
			{
				dRow = (objRow - playerRow1);
				if(dRow == 0) // Colliding					
				{					
					incY = 5;
				}
			}
			
			

			
			if(dCol == 0 && dRow == 0)
			{
				player->getPhysicalProperties()->incX(incX);
			}


		}		
	
		game->getAI()->update(game);		
	}
};